#AVR_TOOLCHAIN = /home/manik/tools/avr/avr8-gnu-toolchain-linux_x86/bin
AVR_TOOLCHAIN = /usr/bin

#Get rid of compiler warning, define F_CPU as 1mhz for atmega16a
F_CPU = 1000000
MCU = atmega16a
board=custom_mega16a


PROGRAM = lcd-hello
CC = $(AVR_TOOLCHAIN)/avr-gcc
CPP = $(AVR_TOOLCHAIN)/avr-g++

OBJCOPY = $(AVR_TOOLCHAIN)/avr-objcopy

# C++ environment
CPP_FLAGS = -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=$(MCU) -DF_CPU=$(F_CPU)
CPP_FLAGS += -DARDUINO=100

CPP_INCLUDES = -I../arduino/hardware/arduino/cores/arduino -I../arduino/hardware/arduino/variants/$(board)
CPP_USR_LIB_INCLUDES = -I../arduino/libraries/LiquidCrystal -I../arduino/libraries/LiquidCrystal/utility
CPP_USR_INCLUDES = -I../arduino/libraries/LiquidCrystal

# C environment
C_FLAGS = -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=$(MCU) -DF_CPU=$(F_CPU)
C_FLAGS += -DARDUINO=100

C_INCLUDES = -I../arduino/hardware/arduino/cores/arduino -I../arduino/hardware/arduino/variants/$(board)
#C_INCLUDES += -I../arduino/libraries/LiquidCrystal -I../arduino/libraries/LiquidCrystal/utility

C_CORE_SRC_DIR = ../arduino/hardware/arduino/cores/arduino
C_CORE_SRC = wiring_analog.c wiring_digital.c wiring_shift.c WInterrupts.c wiring_pulse.c wiring.c

CPP_CORE_SRC_DIR = $(C_CORE_SRC_DIR)
CPP_CORE_SRC = main.cpp Print.cpp WString.cpp

CPP_USR_LIB_SRC_DIR = ../arduino/libraries/LiquidCrystal
CPP_USR_LIB_SRC = LiquidCrystal.cpp

CPP_USR_SRC = lcd_hello.cpp

C_CORE_OBJS = $(C_CORE_SRC:.c=.c_core.o)
C_USR_LIB_OBJS = $(C_USR_LIB_SRC:.c=.c_usr_lib.o)
C_USR_OBJS = $(C_USR_SRC:.c=.c_usr.o)

CPP_CORE_OBJS = $(CPP_CORE_SRC:.cpp=.cpp_core.o)
CPP_USR_LIB_OBJS = $(CPP_USR_LIB_SRC:.cpp=.cpp_usr_lib.o)
CPP_USR_OBJS = $(CPP_USR_SRC:.cpp=.cpp_usr.o)

OBJS= $(C_CORE_OBJS) $(C_USR_LIB_OBJS) $(C_USR_OBJS) $(CPP_CORE_OBJS) $(CPP_USR_LIB_OBJS) $(CPP_USR_OBJS)


 
# Be silent per default, but 'make V=1' will show all compiler calls.
ifeq ($(V),0)
Q := @
endif
 
all: $(PROGRAM).hex
 
$(PROGRAM).elf: $(OBJS)
	avr-ar rcs $(PROGRAM).a $(C_CORE_OBJS) $(CPP_CORE_OBJS)
	avr-gcc -Os -Wl --gc-sections -mmcu=$(MCU) -o $(PROGRAM).elf $(CPP_USR_OBJS) $(C_USR_OBJS) $(CPP_USR_LIB_OBJS) $(C_USR_LIB_OBJS) $(PROGRAM).a -L. -lm
 
$(PROGRAM).hex: $(PROGRAM).elf
	$(Q)$(OBJCOPY) -O ihex $< $@

%.cpp_core.o: $(CPP_CORE_SRC_DIR)/%.cpp
	$(CPP) $(CPP_FLAGS) $(CPP_INCLUDES) -o $@ -c $<

%.cpp_usr_lib.o: $(CPP_USR_LIB_SRC_DIR)/%.cpp
	$(CPP) $(CPP_FLAGS) $(CPP_INCLUDES) $(CPP_USR_LIB_INCLUDES) -o $@ -c $<

%.cpp_usr.o: %.cpp
	$(CPP) $(CPP_FLAGS) $(CPP_INCLUDES) $(CPP_USR_INCLUDES) -o $@ -c $<

%.c_core.o: $(C_CORE_SRC_DIR)/%.c
	$(CC) $(C_FLAGS) $(C_INCLUDES) -o $@ -c $<
	@echo "$(OBJS)"

%.c_usr_lib.o: $(C_USR_LIB_SRC_DIR)/%.c
	$(CC) $(C_FLAGS) $(C_INCLUDES) -o $@ -c $<

%.c_usr.o: %.c
	$(CC) $(C_FLAGS) $(C_INCLUDES) -o $@ -c $<

 
flash: $(PROGRAM).hex
	$(Q)sudo avrdude -p atmega16 -P usb -c avrisp2 -U flash:w:$(PROGRAM).hex -v
	#$(Q)sudo avrdude -c avrispv2 -P usb -p t13 -U flash:w:$(PROGRAM).hex
 
#OBJS= $(C_CORE_OBJS) $(C_USR_LIB_OBJS) $(C_USR_OBJS) $(CPP_CORE_OBJS) $(CPP_USR_LIB_OBJS) $(CPP_USR_OBJS)
clean:
	$(Q)rm -f $(OBJS)
	$(Q)rm -f *.elf
	$(Q)rm -f *.hex
